import {getToken, removeToken, setToken} from "@/utils/auth";
import {login} from "@/api/login";

const state={
    token: getToken()
}
const mutations={
    SET_TOKEN(state,token){
        if(token===null){
            removeToken();
        }else{
            setToken(token.toString())
        }
        state.token=token;
    },
}
const actions={
    login({commit}, form) {
        return new Promise((resolve, reject) => {
            login(form).then((res)=>{
                commit('SET_TOKEN',res.token);
                resolve();
            }).catch(()=>{
                reject();
            })
        })
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
